#include "HostileEntityStates.hpp"
#include "HostileEntity.hpp"
#include "AnimatedSprite.hpp"
#include "../MFSMachine.hpp"
#include "../Particles/EmitterManager.hpp"
#include "../MMessage.hpp"
#include "../MessageDispatcher.hpp"
#include "../SceneManager.hpp"

#include <iostream>


//! HOSTILE_LISTEN
void HostileListen::handleMessage(HostileEntity *pEntity, MMessage *pMsg)
{
	switch(pMsg->getCommand())
	{
	case MSG_CMD_HANDLE_COLL:
		if(pEntity->getStateMachine()->getCurrentState()->getStateName() != HESN_EXPLODE &&
			pEntity->getStateMachine()->getCurrentState()->getStateName() != HESN_DIE)
			pEntity->getStateMachine()->changeState(new HostileExplode());
		//else
		//{
		//	if(pEntity->isVisible())
		//		pEntity->setVisibility(false);				
		//}
		break;

	case MSG_CMD_DIE:
		pEntity->getStateMachine()->changeState(new HostileDie());
		break;

	case MSG_CMD_GET_DAMAGE:
		//if(pEntity->getStateMachine()->getCurrentState()->getStateName() != HESN_GET_DMG)
		//	pEntity->getStateMachine()->changeState(new HostileGetDmg());
		int* iDmg = static_cast<int*>(pMsg->getAddInfo());
		pEntity->sufferDmg(*iDmg);
		if(pEntity->getHitPoints() <= 0)
			pEntity->getStateMachine()->changeState(new HostileDie());
		break;
	}
}

void HostileListen::beginState(HostileEntity *)
{
}

void HostileListen::updateState(HostileEntity *, clock_t)
{
}

void HostileListen::endState(HostileEntity *)
{
}

//! HOSTILE_IDLE
void HostileIdle::beginState(HostileEntity *)
{
}

void HostileIdle::updateState(HostileEntity *, clock_t)
{
}

void HostileIdle::endState(HostileEntity *)
{
}

//! HOSTILE_MOVE
void HostileMove::beginState(HostileEntity *)
{
}

void HostileMove::updateState(HostileEntity *, clock_t)
{
}

void HostileMove::endState(HostileEntity *)
{
}

//! HOSTILE_ATTACK
void HostileAttack::beginState(HostileEntity *)
{
}

void HostileAttack::updateState(HostileEntity *, clock_t)
{
}

void HostileAttack::endState(HostileEntity *)
{
}

////! HOSTILE_GET_DMG
//void HostileGetDmg::beginState(HostileEntity* pEntity)
//{
//}
//
//void HostileGetDmg::updateState(HostileEntity* pEntity, clock_t tick)
//{
//}
//
//void HostileGetDmg::endState(HostileEntity*)
//{
//}

//! HOSTILE_EXPLODE
HostileExplode::~HostileExplode()
{
}

void HostileExplode::beginState(HostileEntity *pEntity)
{
	m_pExplodeAnim = new AnimatedSprite("expl2.png", 11, 75, const_cast<MVector3D&>(pEntity->getPosition()), false, 2.0f, 2.0f);
	SceneManager::GetInstance()->RegisterEntity(m_pExplodeAnim);
}

void HostileExplode::updateState(HostileEntity* pEntity, clock_t)
{
	if(m_pExplodeAnim->isOver())
		pEntity->getStateMachine()->changeState(new HostileDie());
}

void HostileExplode::endState(HostileEntity* pEntity)
{
	SceneManager::GetInstance()->UnregisterEntity(m_pExplodeAnim->getEntityID());
	//MMessage* pMsg = new MMessage(MSG_CMD_DIE, 0, NULL);
	//MessageDispatcher::getInstance()->dispatchMessage(pMsg, pEntity->getEntityID(), pEntity->getEntityID());
}

//! HOSTILE_DIE

HostileDie::~HostileDie()
{
}

void HostileDie::beginState(HostileEntity* pEntity)
{
	m_pExplosionEmitter = new BaseEmitter("fx_med.png", 250, 250);
}

void HostileDie::updateState(HostileEntity *pEntity, clock_t)
{
	static bool added = false;
	if(!added)
	{
		added = true;
		EmitterManager::GetInstance()->AddEmitter(m_pExplosionEmitter);
	}

	if(m_pExplosionEmitter->isDepleted())
	{
		EmitterManager::GetInstance()->removeEmitter(m_pExplosionEmitter);
		SceneManager::GetInstance()->UnregisterEntity(pEntity->getEntityID());
		//pEntity->getStateMachine()->changeState(new HostileIdle);
	}
}

void HostileDie::endState(HostileEntity *pEntity)
{
	//pEntity->setVisibility(false);
	//SceneManager::GetInstance()->UnregisterEntity(pEntity->getEntityID());
}

//EOF
